/**
 * 
 * @authors Riche
 * @date    2016-07-06 14:39:22
 * @version 1.1.0
 */
var commentID = $.getUrlVar("id");
var answerID = $.getUrlVar("aid");
var backUrl = $.getUrlVar("back");
var vm=new Vue({
  el:"#app",
  data:{
    title:'用户名',
    userInfo:{},

    scrollbar:{},
    data:[],
    isLoading:true,
    pageIndex :1,
    pageSize : PAGESIZE
  },
  compiled: function(){
    var _self = this;
    _self.loadPhoto();  //调用图片预加载

    // 用户信息
    _self.getUserInfo();

    var arrMyself = _self.$el.querySelectorAll(".chatMyself .context");
    [].forEach.call(arrMyself,function(value,index,context){
        if(value.childNodes[0].nodeName === "IMG"){
          context[index].style.backgroundColor = "#fff";
          context[index].style.textAlign = "right"
        }
    });
    var arrOthers = _self.$el.querySelectorAll(".chatOthers .context");
    [].forEach.call(arrOthers,function(value,index,context){
        if(value.childNodes[0].nodeName === "IMG"){
          context[index].style.backgroundColor = "#fff";
        }
    });

    //评论列表
    var data = {
        pageSize: _self.pageSize,
        pageIndex:_self.pageIndex,
        commentID: commentID,
        answerID: answerID
    };
    data = Common.jsonEncode(data);
    _self.scrollbar = new scrollBar({
        isLoading: _self.isLoading,
        url: "ask_getCommentList",
        type:"get",
        data:data
    });
    _self.loadlist();

  },
  methods:{
    loadPhoto:function(){
        myJS.imgScale('.j_1', '1', '1');
        myJS.ImgFull('.loadImg','images/liby.png');
    },
    loadlist:function(callback){
      var _self = this;
      _self.scrollbar.load(function(pageIndex,data){
        for(var i = 0; i < data.length; i++){
          _self.data.push(data[i]);
        }
        _self.pageIndex = pageIndex;
      },function(isLoading){
        _self.isLoading = isLoading;
      });
      
    },
    reAddReplyComment:function(e,type,timestamp){
      //失败，重新发送
      var _self = this;
      //$(e.currentTarget).addClass('on').siblings().removeClass('on');
      //type:1内容，2图片
      var arrobj = _self.arrayGet(_self.data,'timestamp',timestamp);
      var data = {
          answerID: answerID,
          comment:arrobj[0].comment,
          commentPicture:arrobj[0].commentPicture,
          parentID:commentID
      };
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: 'ask_addComment', datatype: 'json', type: 'get', data: data}, function (data) {
          if(data.error_code==0){
              var obj = data.data[0];
              var code = obj.code;
              var pic = code==1?"moneyPig.png":"lebel.png";
              alert({
                  text: '<img src="images/'+pic+'"/>',
                  description: obj.str,
                  kind: 0
              });
              //改变状态
              arrobj[0].error_code = 0;
          }else{
              alert(data.msg);
          }
      });

    },
    addReplyComment:function(){
      //回复内容
      var _self = this;
      var comment = '',commentPicture='';
      comment = $("#searchword").val();
      if(comment==""){
        alert("请输入评论内容");
        return;
      }
      var data = {
          answerID: answerID,
          comment:comment,
          commentPicture:commentPicture,
          parentID:commentID
      };

      //data = Common.jsonEncode(data);
      _self.ajax(data);

      $("#searchword").val('');
    },
    addReplyPic:function(image){
      //回复图片
      alert("回复图片");
      var _self = this;
      var comment = '',commentPicture='';
      commentPicture = image;
      if(commentPicture==""){
        alert("请选择图片");
        return;
      }
      comment = $("#searchword").val();
      var data = {
          answerID: answerID,
          comment: comment,
          commentPicture:commentPicture,
          parentID:commentID
      };
      //data = Common.jsonEncode(data);
      //追加到data中
      var para = data;
      var obj = {
        mySelf:1,
        comment:para.comment,
        commentPicture:para.commentPicture,
        headPic:_self.userInfo.headPic,
        userName:_self.userInfo.userName,
        uid:_self.userInfo.uid,
        commentDate:'刚刚',
        error_code:data.error_code,
        timestamp :new Date().getTime()
      };
      //_self.data.push(obj);
      _self.ajax(data);
      
      $("#pic").val('');
    },
    ajax:function(data){
        var _self = this;
        var para = data;
        Common.ajaxCall({ url: 'ask_addComment', datatype: 'json', type: 'get', data: data }, function (data) {
            if(data.error_code==0){
                var obj = data.data[0];
                var code = obj.code;
                var pic = code==1?"moneyPig.png":"lebel.png";
                alert({
                    text: '<img src="images/'+pic+'"/>',
                    description: obj.str,
                    kind: 0
                });
            }else{
                alert(data.msg);
            }
            //追加到data中
            var obj = {
              mySelf:1,
              comment:para.comment,
              commentPicture:para.commentPicture,
              headPic:_self.userInfo.headPic,
              userName:_self.userInfo.userName,
              uid:_self.userInfo.uid,
              commentDate:'刚刚',
              error_code:data.error_code,
              timestamp :new Date().getTime()
            };
            _self.data.push(obj);

        });
    },
    getUserInfo:function(){
      //用户信息
      var _self = this;
      var data = {
      };
      data = Common.jsonEncode(data);
      Common.ajaxCall({ url: 'user_getUserInfo', datatype: 'json', type: 'get', data: data}, function (data) {
          if(data.error_code==0){
              var obj = data.data;
              _self.userInfo = obj;
          }else{
              alert(data.msg);
          }
      });
    },
    arrayGet:function(arr,objPropery,objValue){
      //从对象数组中获取属性为objPropery，值为objValue元素的对象
      return $.grep(arr, function(cur,i){
          return cur[objPropery]==objValue;
       });
    },
    arrayRemove:function(arr,objPropery,objValue){
      //从对象数组中删除属性为objPropery，值为objValue元素的对象
      return $.grep(arr, function(cur,i){
          return cur[objPropery]!=objValue;
       });
    },
    selectPic:function(){
      //选择图片
      $("#pic").click();
    },
    back:function(){
      window.location.href = decodeURIComponent(backUrl);
    }
  },
  watch:{
      "data": function(){
          this.loadPhoto();
          $(document).scrollTop($(document).height());
      }
  }
});